package cn.iocoder.yudao.module.cms.article.controller.admin;

import cn.iocoder.yudao.framework.business.category.pojo.form.CategorySaveDTO;
import cn.iocoder.yudao.framework.business.category.pojo.vo.CategorySimpleVO;
import cn.iocoder.yudao.framework.business.category.pojo.vo.CategoryVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.cms.article.service.ArticleCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 文章分类")
@RestController
@RequestMapping("/cms/article-category")
@Validated
public class ArticleCategoryController {

    @Resource
    private ArticleCategoryService articleCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建分类")
    @PreAuthorize("@ss.hasPermission('cms:article-category:create')")
    public CommonResult<Long> create(@Valid @RequestBody CategorySaveDTO dto) {
        return success(articleCategoryService.create(dto));
    }

    @PostMapping("/update")
    @Operation(summary = "更新分类")
    @PreAuthorize("@ss.hasPermission('cms:article-category:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody CategorySaveDTO updateReqVO) {
        return success(articleCategoryService.update(updateReqVO));
    }

    @GetMapping("/delete")
    @Operation(summary = "删除分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:article-category:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        return success(articleCategoryService.delete(id));
    }

    @GetMapping("/get")
    @Operation(summary = "获得分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:article-category:query')")
    public CommonResult<CategoryVO> get(@RequestParam("id") Long id) {
        CategoryVO category = articleCategoryService.getDetail(id);
        return success(category);
    }

    @GetMapping("/list")
    @Operation(summary = "获取分类列表")
    @PreAuthorize("@ss.hasPermission('cms:article-category:query')")
    public CommonResult<List<CategoryVO>> getCategoryList() {
        List<CategoryVO> list = articleCategoryService.getAllList();
        return success(list);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获取分类精简信息列表", description = "只包含被开启的分类，主要用于前端的下拉选项")
    @PermitAll
    public CommonResult<List<CategorySimpleVO>> getSimpleCategoryList() {
        // 获得分类列表，只要开启状态的
        List<CategorySimpleVO> list = articleCategoryService.getSimpleList();
        return success(list);
    }

}
